﻿using GuanQL.Core;
using GuanQL.Core.Domain.Accounts;
using GuanQL.Core.Multitenancy;
using GuanQL.Core.Stores;
using GuanQL.Service.Accounts;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Reflection;
using ZhongJia.Community.Service.Accounts;
using ZhongJia.Core;
using ZhongJia.Core.Caching;
using ZhongJia.Core.Messaging;

namespace GuanQL.Service
{
    public class Bootstrapper
    {
        public static void RegisterServices(IServiceCollection services, IConfiguration config)
        {
            services.AddScoped<DbContext, GQLDbContext>();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped<IWebContext, WebContext>();
            services.AddScoped<ITenantResolver, DefaultTenantResolver>();
            services.AddSingleton<IMessageBus, InMemoryMessageBus>();
            services.AddSingleton<ICacheClient, InMemoryCacheClient>();
            services.AddScoped<GQLUserManager<User>>();
            services.AddScoped<GQLSignInManager<User>>();
            services.AddZhongJiaApp(config);
           // services.AddSingleton<TcpServer>();
            AddCustomServices(services);
        }

        private static void AddCustomServices(IServiceCollection services)
        {
            var allTypes = Assembly.Load(new AssemblyName("GuanQL.Service")).GetTypes();
            foreach (var service in allTypes.Where(t => typeof(IService).IsAssignableFrom(t)))
            {
                services.AddScoped(service);
            }
        }
    }
}
